﻿Shader "Dan/UI/UI_ArrowMove02"
{
    Properties
    {
        _Color ("Color", Color) = (0.1,0.4,0.8,1)
        _Color2 ("Color2", Color) = (1,0,0,1)
        _Emission ("Emission", Float ) = 1
        _Level ("Level", Float ) = 4
        _CenterFactor ("Center Factor", Float ) = 1
        _Width ("Width", Range(0, 1)) = 0
        _Percentage ("Percentage", Range(0, 1)) = 0
        [MaterialToggle] _UseTime ("UseTime", Float ) = 0
        _TimeSpeed ("TimeSpeed", Float ) = 0.5
        [MaterialToggle] _UsePingpong ("UsePingpong", Float ) = 0
        _BG_Opacity ("BG_Opacity", Range(0, 1)) = 0.1
        
    }
    SubShader
    {
        Tags { "IgnoreProjector"="True" "Queue"="Transparent" "RenderType"="Transparent" }   
        LOD 100

        Pass
        {
            
            Blend SrcAlpha OneMinusSrcAlpha
            ZWrite Off    
           
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;              
                float4 vertex : SV_POSITION;
            };

            float4 _Color;
            float _TimeSpeed;
            float _Level;
            float _CenterFactor;
            float _Width;
            fixed _UseTime;
            fixed _UsePingpong;
            float _Percentage;
            float4 _Color2;
            float _Emission;
            float _BG_Opacity;
           
            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv; 
                return o;
            }


            fixed4 frag (v2f i) : SV_Target
            {
                
                float2 uv =i.uv;//主UV
                //分层和中心
                float levelAmount = _Level+1;
                float st = floor(abs(levelAmount));
                float center = abs(uv.y*2.0-1.0)*_CenterFactor;
                //求三角形状
                float shape01 = uv.x*st+center-1.0;
                float shape02 = (1.0 - uv.x)*st-center;
                float arrow = step(_Width,frac(shape01));
               
                //求遮罩
                float maskStart = ceil(shape01);
                float maskEnd =ceil(shape02);
                float maskLeft = saturate(maskStart);
                float maskRight = saturate(maskEnd);
                float mask = arrow*maskRight*maskLeft;
               
                //黑白渐变
                float rampVal = maskStart/levelAmount;
                //颜色渐变
                float3 finalColor = lerp(_Color.rgb,_Color2.rgb,rampVal)*_Emission;

                //闪烁
                float flicker = frac( _Time.y*_TimeSpeed);
                float pingpongTime =lerp( flicker, 1 - abs(flicker*2-1), _UsePingpong);
                float flickerTime =lerp( _Percentage, pingpongTime, _UseTime );
                //渐变滚动
                float rampFlow = step(rampVal,flickerTime)+_BG_Opacity;
                
                return fixed4(finalColor,rampFlow*mask);
                
            }
            ENDCG
        }
    }
}
